package com.jewaton.auth.commons.dao;

import com.jewaton.auth.commons.entity.District;
import com.jewaton.auth.commons.entity.Organization;
import com.jewaton.auth.commons.util.StringUtil;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Repository(value="organizationDAO")
public class OrganizationDAO extends BaseDAO{
	
	public Organization getOrgById(String id){
	    return (Organization) findObjectById(Organization.class, id);
	}
	
	
	/**
     * 根据区域id 模糊匹配 区域 
     * @param did
     * @return
     */
    public List<District> getFuzzyDistrictByDid(String did) {
        StringBuffer hql = new StringBuffer("from District d where 1=1 ");
        Map<String,Object> param = new HashMap<String,Object>();
        if (!StringUtil.isBlank(did)) {
            hql.append(" and d.did like :did");
            param.put("did",   did + "%");
        }
        hql.append(" order by d.did ");
        return this.createQuery(hql.toString(), param).list();
    }
    /**
     * 根据父区域id 模糊匹配 区域 
     * @param parentId
     * @return
     */
    public List<District> getFuzzyDistrictByParentId(String parentId) {
        StringBuffer hql = new StringBuffer("from District d where 1=1 ");
        Map<String,Object> param = new HashMap<String,Object>();
        if (!StringUtil.isBlank(parentId)) {
            hql.append(" and d.parentId like :parentId");
            param.put("parentId", parentId + "%");
        }
        hql.append(" order by d.did ");
        return this.createQuery(hql.toString(), param).list();
    }
    public List<District> getFuzzyDistrictByParentId(String parentId,String did) {
    	StringBuffer hql = new StringBuffer("from District d where 1=1 ");
    	Map<String,Object> param = new HashMap<String,Object>();
    	if (!StringUtil.isBlank(parentId)) {
    		hql.append(" and (d.parentId like :parentId or did =:did)");
    		param.put("parentId", parentId + "%");
    		param.put("did", did);
    	}
    	hql.append(" order by d.did ");
    	return this.createQuery(hql.toString(), param).list();
    }
    
    /**
	 * 根据用户Aid查询该用户所属地区
	 * @return
	 */
	public List<String> getDisIDByAid(Long aid){
		String sql="select dis.DID as did from DISTRICT dis inner join ORGANIZATION org on org.DID=dis.DID inner join ACCOUNT a on a.ORG_ID=org.OID where a.AID="+aid;
		return getSQLList(sql,-1,-1);
	}
	
    /**
     * 根据区域 精确匹配 组织  
     * @param did
     * @return
     */
    public List<Organization> getOrgByDid(String did) {
            StringBuffer hql = new StringBuffer("from Organization o where 1=1 ");
            Map<String, Object> param = new HashMap<String, Object>();
            if (!StringUtil.isBlank(did)) {
                hql.append(" and o.district.did = :did");
				param.put("did", did);
            }
            hql.append(" order by o.oid ");
            return this.createQuery(hql.toString(), param).list();
      }
    
    
    /**
	 * 根据区域id 模糊匹配 组织 
	 * @param did
	 * @return
	 */
	public List<Organization> getFuzzyOrgByDid(String did) {
	    StringBuffer hql = new StringBuffer("from Organization o where 1=1 ");
	    Map<String, Object> param = new HashMap<String, Object>();
	    if (!StringUtil.isBlank(did)) {
	        hql.append(" and o.district.did like :did");
	        param.put("did", did +  "%");
	    }
	    hql.append(" order by o.oid ");
	    return this.createQuery(hql.toString(), param).list();
	}

	/**
	 * 根据组织id 模糊匹配 组织 
	 * @param oid
	 * @return
	 */
	public List<Organization> getFuzzyOrgByOid(String oid) {
	    StringBuffer hql = new StringBuffer("from Organization o where 1=1 ");
	    Map<String, Object> param = new HashMap<String, Object>();
	    if (!StringUtil.isBlank(oid)) {
	        hql.append(" and o.oid like :oid ");
	        param.put("oid", oid +  "%");
	    }
	    hql.append(" order by o.oid ");
	    return this.createQuery(hql.toString(), param).list();
	}
	
	public Organization findOrganizationById(String oid){
		Organization org=null;
		if(oid!=null){
			String hql = "from Organization org where org.oid = :oid ";
			Query query = this.getSession().createQuery(hql);
			query.setString("oid", oid);
			List<Organization>list=query.list();
			if(list.size()>0 && !list.isEmpty()){
				org=list.get(0);
			}
		}		
		return org;
	}
	
	public List<Organization> getFuzzyOrgByOid2(String oid) {
		StringBuffer hql = new StringBuffer("from Organization o where o.oid !='001' ");
		Map<String, Object> param = new HashMap<String, Object>();
		if (!StringUtil.isBlank(oid)) {
			hql.append(" and o.oid like :oid  ");
			param.put("oid", oid +  "%");
		}
		hql.append(" order by o.oid ");
		return this.createQuery(hql.toString(), param).list();
	}
}
